package com.enpronomics.pcall.receivers;

import java.util.Timer;
import java.util.TimerTask;
import java.util.prefs.Preferences;

import org.apache.http.util.LangUtils;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Handler;
import android.widget.Toast;

import com.enpronomics.pcall.MyLocation;
import com.enpronomics.pcall.MyLocation.LocationResult;
import com.enpronomics.pcall.GetLocation;
import com.enpronomics.pcall.ServiceManager;
import com.enpronomics.pcall.activity.DialogActivity;
import com.enpronomics.pcall.activity.PanicStartActivity;

public class PanicReceiver extends BroadcastReceiver {
	public static Context context;
	//private final Handler handler = new Handler();
	private Timer timer = new Timer();
	
	private int nCounter = 0;
	private int safeModeCounter = 0;
	
	private TimerTask smsTimerTask;
	private int nCounter2=0;

	@Override
	public void onReceive(final Context context, Intent intent) {

		this.context=context;
		safeModeCounter++;
		if(safeModeCounter==1){
			timer.purge();
			timer.schedule(new TimerTask() {
				public void run() {
					nCounter++;
					if(nCounter > 5){
						safeModeCounter=0;
						nCounter=0;
						
						cancel();
					}else if (safeModeCounter >= 3) {
						safeModeCounter=0;
						nCounter=0;
						try{smsTimerTask.cancel();}catch (Exception e) {}
						cancel();
						StartPanic();
					}
				}
			}, 100, 1000 );
		}
	}

	private String panicSMSNumber;
	private String panicCallNumber;
	private String PositionReportNumber;
	private String messageType, name;
	private int repetition, interval;
	private boolean panicInitiated;
	private void StartPanic(){
		
//		new SendLocationSMS(context);
		if (ServiceManager.getBoolSetting(context, ServiceManager.KEY_PANIC)) {
			
			panicSMSNumber = ServiceManager.getStringSetting(context,ServiceManager.KEY_PANIC_SMS_NUMBER);
			panicCallNumber = ServiceManager.getStringSetting(context,ServiceManager.KEY_PANIC_CALL_NUMBER);
			PositionReportNumber = ServiceManager.getStringSetting(context,ServiceManager.KEY_POSITION_REPORT_NUMBER);
			name = ServiceManager.getStringSetting(context,ServiceManager.KEY_NAME);
			repetition = ServiceManager.getIntSetting(context,ServiceManager.KEY_SOS_REPETITION);
			interval = ServiceManager.getIntSetting(context,ServiceManager.KEY_ACTION_INTERVAL);
			
			if (!panicCallNumber.equals("None")) {
				//!panicInitiated) {
					ServiceManager.setBoolSetting(context,ServiceManager.PANIC_INITIATED, true);
					ServiceManager.call(context,panicCallNumber);
//					Toast.makeText(context, "Panic Initiated", Toast.LENGTH_LONG).show();
					
					smsTimerTask=new TimerTask() {
						public void run() {
							if (nCounter2 == repetition
									|| !ServiceManager.getBoolSetting(context,ServiceManager.PANIC_INITIATED)) {
								ServiceManager.setBoolSetting(context,ServiceManager.PANIC_INITIATED, false);
								smsTimerTask.cancel();
//								Toast.makeText(context,"Panic Finished", Toast.LENGTH_LONG).show();
							}
							else{
								nCounter2++;
								Intent serviceIntent=new Intent(context, PanicService.class);
								serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						        context.startService(serviceIntent);  
							}
						}
					};
					new Timer().schedule(smsTimerTask, 100, interval * 1000 * 60);
				}
//			        else {
//					ServiceManager.setBoolSetting(context,ServiceManager.PANIC_INITIATED, false);
//					if (mTimerTask != null) {
//						mTimerTask.cancel();
//						Toast.makeText(context, "Panic Stopped", Toast.LENGTH_LONG).show();
//					}
//				}
		}
		
//		Intent launch = new Intent(context,DialogActivity.class);
//		launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//		launch.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//		context.startActivity(launch);

	}
}
